package com.voxel.sense.infomsg.widget.adapter;

import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.widget.CommonImagePagerActivity;
import com.voxel.sense.infomsg.data.bean.FirmTableInfoBean;
import cyy.example.com.facotry.Factory;

import java.util.ArrayList;

/**
 * 公司表格适配器
 * Created by chenyiyao on 2017/11/1.
 */

public class FirmTableRecyclerAdapter extends RecyclerAdapter<FirmTableInfoBean> {

    @Override
    protected int getItemViewType(int position, FirmTableInfoBean tableInfoBean) {
        int resId = R.layout.item_recycler_firm_table_head;
        if (FirmTableInfoBean.TYPE_HEAD_EXPAND==tableInfoBean.getType()
                || FirmTableInfoBean.TYPE_HEAD==tableInfoBean.getType()
                || FirmTableInfoBean.TYPE_URL==tableInfoBean.getType()) {
            resId = R.layout.item_recycler_firm_table_head;

        } else if (FirmTableInfoBean.TYPE_HEAD_SECOND==tableInfoBean.getType()) {
            resId = R.layout.item_recycler_firm_table_head_second;
        } else if (FirmTableInfoBean.TYPE_NORMAL==tableInfoBean.getType()) {
            resId = R.layout.item_recycler_firm_table_third;
        }
        return resId;
    }


    public FirmTableRecyclerAdapter( AdapterListener<FirmTableInfoBean> listener) {
        super( listener);
    }

    @Override
    protected ViewHolder<FirmTableInfoBean>  onCreateViewHolder(View root, int viewType) {
        ViewHolder<FirmTableInfoBean>   viewholder = new ViewholderThird(root);
        if (R.layout.item_recycler_firm_table_head == viewType) {
            viewholder = new Viewholder(root);
        } else if (R.layout.item_recycler_firm_table_head_second == viewType) {
            viewholder = new ViewholderHeadSecond(root);
        } else if (R.layout.item_recycler_firm_table_third == viewType) {
            viewholder = new ViewholderThird(root);
        }
        return viewholder;
    }

    static class Viewholder extends ViewHolder<FirmTableInfoBean> {

        TextView tv_desc;
        TextView tv_value;
        View root;

        private Viewholder(View itemView) {
            super(itemView);
            tv_desc =  itemView.findViewById(R.id.tv_desc);
            tv_value =  itemView.findViewById(R.id.tv_value);
            root = itemView;
        }

        @Override
        protected void onBind(final FirmTableInfoBean bean) {
            tv_desc.setText(bean.getDescribe());
            tv_value.setText(bean.getValue());
            if (FirmTableInfoBean.TYPE_HEAD_EXPAND==bean.getType()) {
                root.setBackground(Factory.app().getResources().getDrawable(R.color.table_head_bg_grey));
            } else {
                root.setBackground(Factory.app().getResources().getDrawable(R.color.table_second_bg_grey));
            }

            if (FirmTableInfoBean.TYPE_URL==bean.getType()) {
                //由于这里是连接，文本不需要直接显示连接地址
                tv_value.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
                tv_value.getPaint().setAntiAlias(true);//抗锯齿
                if (!TextUtils.isEmpty(bean.getValue())) {
                    tv_value.setText("点击查看");
                }
            } else {
                tv_value.getPaint().setFlags(0);
            }

            tv_value.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (FirmTableInfoBean.TYPE_URL==bean.getType() && !TextUtils.isEmpty(bean.getValue())) {
                        String value = bean.getValue();
                        ArrayList<String> list = new ArrayList<>();
                        list.add(bean.getValue());
                        //先判断是图片还是psd文件，然后再根据类型进行操作
                        CommonImagePagerActivity.showImages(root.getContext(), list, 0);
                    }
                }
            });
        }
    }

    static class ViewholderHeadSecond extends ViewHolder<FirmTableInfoBean> {

        TextView tv_desc;

        private ViewholderHeadSecond(View itemView) {
            super(itemView);
            tv_desc =  itemView.findViewById(R.id.tv_desc);
        }

        @Override
        protected void onBind(FirmTableInfoBean bean) {
            tv_desc.setText(bean.getDescribe());
        }
    }

    static class ViewholderThird extends ViewHolder<FirmTableInfoBean> {
        TextView tv_desc;
        TextView tv_value;

        private ViewholderThird(View itemView) {
            super(itemView);
            tv_desc =  itemView.findViewById(R.id.tv_desc);
            tv_value =  itemView.findViewById(R.id.tv_value);
        }

        @Override
        protected void onBind(FirmTableInfoBean bean) {
            tv_desc.setText(bean.getDescribe());
            tv_value.setText(bean.getValue());
        }
    }
}
